<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"></script>
    <style>
        /* 自定义动画样式 */
        .alert {
            animation: fadeInOut 1s;
        }
        @keyframes fadeInOut {
            0% {opacity: 0;}
            50% {opacity: 1;}
            100% {opacity: 0;}
        }
    </style>
</head>
<body>
<div class="container mt-5">
    <h2 class="text-center mb-4">用户注册</h2>
    <div id="message" class="alert alert-danger alert-dismissible fade hide" role="alert">
        注册失败，请重试。
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <form id="registerForm">
        <div class="mb-3">
            <label for="name" class="form-label">姓名</label>
            <input type="text" class="form-control" id="name" required>
        </div>
        <div class="mb-3">
            <label for="phone" class="form-label">电话号码</label>
            <input type="tel" class="form-control" id="phone" required>
        </div>
        <div class="mb-3">
            <label for="password" class="form-label">密码</label>
            <input type="password" class="form-control" id="password" required>
        </div>
        <div class="mb-3">
            <label for="confirmPassword" class="form-label">确认密码</label>
            <input type="password" class="form-control" id="confirmPassword" required>
        </div>
        <button type="submit" class="btn btn-primary">注册</button>
    </form>
</div>

<script>
    // 使用jQuery处理表单提交
    $(document).ready(function(){
        $("#registerForm").on("submit", function(event){
            event.preventDefault(); // 阻止默认提交行为
            let name = $("#name").val();
            let phone = $("#phone").val();
            let password = $("#password").val();
            let confirmPassword = $("#confirmPassword").val();

            // 简单验证密码是否一致
            if(password !== confirmPassword) {
                alert("两次输入的密码不一致！");
                return;
            }

            // 使用GET方式提交数据
            fetch("./register?name=" + encodeURIComponent(name)
                + "&phone=" + encodeURIComponent(phone)
                + "&password=" + encodeURIComponent(password), {
                method: "GET"
            })
                .then(response => response.text())
                .then(data => {
                    if(data === "success") {
                        window.location.href = "login.html";
                    } else {
                        $("#message").removeClass("hide").addClass("show");
                        setTimeout(() => {
                            $("#message").removeClass("show").addClass("hide");
                        }, 900); // 动画结束后隐藏消息
                    }
                })
                .catch(error => console.error("Error:", error));
        });
    });
</script>
</body>
</html>
